package git.soulbgm.security.handler;

import git.soulbgm.common.constant.HttpStatus;
import git.soulbgm.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 访问被拒绝的处理类
 *
 * @author SoulBGM
 * @date 2020-08-05
 */
@Slf4j
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.info("请求访问:{}，访问被拒绝，权限不足", request.getRequestURI());

        if (!response.isCommitted()) {
            ResponseUtil.renderError(response, HttpStatus.FORBIDDEN, "访问被拒绝，权限不足");
        }
    }

}
